<template>
  <div class="app-container">
    
    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="课程名字" align="center" prop="courseName" />
      <el-table-column label="周几" align="center" prop="week" />
      <el-table-column label="开始小时" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束小时" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.endTime}}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="courseType" >
         <template >
          <span>正常</span>
        </template>
      </el-table-column>
      <el-table-column label="老师名称" align="center" prop="teacherName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="课程名字" prop="courseName">
          <!-- <el-input v-model="form.courseName" placeholder="请输入课程名字" /> -->
          <SearchCourseName ref="SearchCourseName" @update="updateCourseInfo" />
        </el-form-item>
        <el-form-item label="星期" prop="week">
          <el-select v-model="form.week" placeholder="请选择星期">
            <el-option
              v-for="dict in dict.type.sys_week_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker
            size="small"
            clearable
            v-model="form.startTime"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
            placeholder="选择开始时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-picker
            size="small"
            clearable
            v-model="form.endTime"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
            placeholder="选择结束时间">
          </el-time-picker>
        </el-form-item>
        
        <el-form-item label="课程状态" prop="courseType">
          <el-select v-model="form.courseType" placeholder="请选择课程状态">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="取消" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="授课老师" prop="teacherId">
          <!-- <el-input v-model="form.teacherId" placeholder="请输入授课老师" /> -->
          <SearchTeacher ref="SearchTeacherName" @change="updateTeacherInfo" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCourseByName, getCourseDeatil, updateSccourse, addSccourse, delSccourse } from "@/api/school/sccourse";
import SearchCourseName from '../model/searchCourseName.vue'
import SearchTeacher from '../model/searchTeacher'

export default {
  name: "List",
  dicts: ['sys_week_type'],
  components: {SearchCourseName, SearchTeacher},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: this.$route.query.name,
        week: null,
        startTime: null,
        endTime: null,
        classId: null,
        courseType: null,
        teacherId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名字不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    open: function(newV, oldV) {
      if (newV) {
          this.$nextTick(() => {
            console.log(this.form)
            if (this.title.search(/修改/g) > -1 ) {
              this.$refs.SearchCourseName.init(this.form.classId)
              this.$refs.SearchTeacherName.init(this.form.teacherId)
            } else {
              this.$refs.SearchCourseName.init()
              this.$refs.SearchTeacherName.init()
            }
          })
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    updateCourseInfo(e) {
      this.form.classId = e
    },
    updateTeacherInfo(e) {
      this.form.teacherId = e
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      getCourseByName(this.queryParams).then(response => {
        this.listList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseName: null,
        week: null,
        startTime: null,
        endTime: null,
        classId: null,
        createTime: null,
        updateTime: null,
        courseType: null,
        teacherId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourseDeatil(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form)
          if (this.form.id != null) {
            updateSccourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSccourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程？').then(() => {
        return delSccourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有课程数据项？').then(() => {
        this.exportLoading = true;
        return exportList(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
